package com.zb.controller;

import com.zb.dto.BrandDto;
import com.zb.dto.CategoryDto;
import com.zb.dto.SkuDto;
import com.zb.dto.SpuDto;
import com.zb.entity.TbBrandModel;
import com.zb.entity.TbCategoryModel;
import com.zb.entity.TbSkuModel;
import com.zb.entity.TbSpuModel;
import com.zb.service.TbBrandService;
import com.zb.service.TbCategoryService;
import com.zb.service.TbSkuService;
import com.zb.service.TbSpuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/tb-sku-model")
public class TbSkuController {

    @Autowired
    private TbSkuService tbSkuModelService;
    @Autowired
    private TbSpuService tbSpuService;
    @Autowired
    private TbBrandService tbBrandService;
    @Autowired
    private TbCategoryService tbCategoryService;

    @PutMapping("/updateGoodsNum/{skuId}/{num}")
    public Integer updateGoodsNum(@PathVariable("skuId")String skuId,@PathVariable("num")Integer num){
        TbSkuModel skuModel = tbSkuModelService.getById(skuId);
        if (skuModel != null && num > 0 &&skuModel.getNum() < num){
            return 0;
        }
//        int a = 10 / 0;
        skuModel.setNum(skuModel.getNum()-num);
        tbSkuModelService.updateById(skuModel);
        return 1;
    }


    @GetMapping("/info/{skuId}")
    public Map<String, Object> info(@PathVariable("skuId") String skuId) {
        Map<String, Object> map = new HashMap<>();
        TbSkuModel sku = tbSkuModelService.getById(skuId);
        TbSpuModel spu = tbSpuService.getById(sku.getSpuId());
        TbCategoryModel c1 = tbCategoryService.getById(spu.getCategory1Id());
        TbCategoryModel c2 = tbCategoryService.getById(spu.getCategory2Id());
        TbCategoryModel c3 = tbCategoryService.getById(spu.getCategory3Id());
        TbBrandModel brand = tbBrandService.getById(spu.getBrandId());
        SkuDto skuDto = new SkuDto();
        SpuDto spuDto = new SpuDto();
        CategoryDto cd1 = new CategoryDto();
        CategoryDto cd2 = new CategoryDto();
        CategoryDto cd3 = new CategoryDto();
        BrandDto brandDto = new BrandDto();
        BeanUtils.copyProperties(sku, skuDto);
        BeanUtils.copyProperties(spu, spuDto);
        BeanUtils.copyProperties(c1, cd1);
        BeanUtils.copyProperties(c2, cd2);
        BeanUtils.copyProperties(c3, cd3);
        BeanUtils.copyProperties(brand, brandDto);

        map.put("sku", skuDto);
        map.put("spu", spuDto);
        map.put("c1", cd1);
        map.put("c2", cd2);
        map.put("c3", cd3);
        map.put("brand", brandDto);
        return map;
    }


}
